/*
 * @Author: qsy
 * @Date: 2025-04-09 20:11:29
 * @LastEditors: qsy
 * @LastEditTime: 2025-04-10 09:21:09
 * @Description: 请填写简介
 */
#include "my_io.h"
#include "driver/ledc.h"

#define LEDC_CHANNEL_0 LEDC_CHANNEL_0
#define LEDC_CHANNEL_1 LEDC_CHANNEL_1
#define LEDC_CHANNEL_2 LEDC_CHANNEL_2

#define LEDC_TIMER_0 LEDC_TIMER_0
#define LEDC_FREQUENCY 5000
#define LEDC_RESOLUTION LEDC_TIMER_13_BIT

void my_io_init(void){
    gpio_pad_select_gpio(LED_GPIO);
    gpio_set_direction(LED_GPIO, GPIO_MODE_OUTPUT);

    gpio_pad_select_gpio(MOTOR_GPIO);
    gpio_set_direction(MOTOR_GPIO, GPIO_MODE_OUTPUT);

    // 拉高MOTOR_GPIO
    // gpio_set_level(MOTOR_GPIO, 1);

    gpio_pad_select_gpio(FENGSAN_GPIO);
    gpio_set_direction(FENGSAN_GPIO, GPIO_MODE_OUTPUT);

    // PWM配置
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .timer_num = LEDC_TIMER_0,
        .duty_resolution = LEDC_RESOLUTION,
        .freq_hz = LEDC_FREQUENCY,
        .clk_cfg = LEDC_AUTO_CLK,
    };
    ledc_timer_config(&ledc_timer);

    ledc_channel_config_t ledc_channel = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .channel = LEDC_CHANNEL_0,
        .timer_sel = LEDC_TIMER_0,
        .intr_type = LEDC_INTR_DISABLE,
        .gpio_num = LED_GPIO,
        .duty = 0, // 初始占空比0%
        .hpoint = 0,
    };
    ledc_channel_config(&ledc_channel);
}

// 参数范围是0-8191
void set_led_brightness(uint32_t brightness){
    ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, brightness);
    ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
}

/**
 * @description: 操作动作
 * @return {*}
 */
void operate_action(gpio_num_t GPIO_ACTION, bool operation){
    if(operation){
        gpio_set_level(GPIO_ACTION, 1);
    }else{
        gpio_set_level(GPIO_ACTION, 0);
    }
}